function [var1_arr,var2_arr,pb_stim,pb_runP,pb_velM,frame_times_arr]=pb_calc(stim_id,velM_ind,velP_ind,ids,l_win,r_win,data_lwin,data_rwin,shutterOpen,counter,var1,var2)
count=sum([l_win r_win 1]);
stim_id(1:shutterOpen(counter))=0;
stim_id(shutterOpen(counter+1)+1:end)=0;
var1_arr={};
var2_arr={};
pb_stim={};
pb_velM={};
pb_runP={};
frame_times_arr={};
curr_onsets=[];
for bnd=1:length(ids)
    curr_onsets=find(diff(stim_id==ids(bnd))==1);
    curr_snips=[];
    curr_onsets(curr_onsets-l_win<0)=[];
    curr_onsets(curr_onsets+r_win>length(stim_id))=[];
    curr_onsets(stim_id(curr_onsets)>0)=[];
    cnt=0;
    gnd=1;
    for knd=1:length(curr_onsets)
        if sum(velP_ind(curr_onsets(knd)-l_win:curr_onsets(knd)+r_win))==count;
            if sum(velM_ind(curr_onsets(knd)-l_win:curr_onsets(knd)+r_win))==0;
                var1_arr{bnd}(:,gnd)=var1(curr_onsets(knd)-data_lwin:curr_onsets(knd)+data_rwin);
                var2_arr{bnd}(:,gnd)=var2(curr_onsets(knd)-data_lwin:curr_onsets(knd)+data_rwin);
                pb_stim{bnd}(:,gnd)=stim_id(curr_onsets(knd)-data_lwin:curr_onsets(knd)+data_rwin);
                pb_velM{bnd}(:,gnd)=velM_ind(curr_onsets(knd)-data_lwin:curr_onsets(knd)+data_rwin);
                pb_runP{bnd}(:,gnd)=velP_ind(curr_onsets(knd)-data_lwin:curr_onsets(knd)+data_rwin);
                frame_times_arr{bnd}(:,gnd)=curr_onsets(knd)-data_lwin:curr_onsets(knd)+data_rwin;
                gnd=gnd+1;
            else
                cnt=cnt+1;
            end
        else
            cnt=cnt+1;
        end
        if cnt==length(curr_onsets)
            var1_arr{bnd}=[];
            var2_arr{bnd}=[];
            pb_stim{bnd}=[];
            pb_velM{bnd}=[];
            pb_runP{bnd}=[];
            frame_times_arr{bnd}=[];
%             display(['no instances of ' num2str(ids(bnd)) ' perturbation']);
        end
    end
    if length(curr_onsets)==0
        var1_arr{bnd}=[];
        var2_arr{bnd}=[];
        pb_stim{bnd}=[];
        pb_velM{bnd}=[];
        pb_runP{bnd}=[];
        frame_times_arr{bnd}=[];
    end
end